package com.learn.demo.netty.io;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * byte转long解码器
 *
 * @author banjiawei
 * @date 2021/08/03
 */
public class MyByteToLongDecoder extends ByteToMessageDecoder {

    /**
     * 解码
     * @author banjiawei
     * @date 2021/8/3
     * @param channelHandlerContext 上下文
     * @param byteBuf 入站byte缓存
     * @param list 将解码的数据传入下一个handler
     * @return void
     */
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        System.out.println("MyByteToLongDecoder 被调用");
        //一个long 8个字节
        if(byteBuf.readableBytes() >= 8){
            list.add(byteBuf.readLong());
        }
    }
}
